﻿using System;
using System.Threading;

using HostPc;

namespace HostPc
{
    namespace AWSIOTCoreSDK
    {
        /**
         * AWSIOTCore的内部操作类**/
        class Application
        {
            private static ManualResetEvent manualResetEvent;
            /**
             * 查看远程remote设备是否还在线。**/
            public bool IsOnduty(ref AWSIOTCoreSDK.Adapter adapter)
            {
                if (adapter.mqtt_client == null)
                    return false;
                if (!adapter.mqtt_client.IsConnected)
                    return false;
                
                // 收到的新消息和上一个信息时间差小于60分钟，则认为此远控端在线,否者认为remote端此时无人使用
                DateTime now_time = DateTime.Now;
                int no_sub_time = CalSpanMinutes(adapter.hostpc_config.remote_last_alive, now_time);
                if ((no_sub_time < adapter.hostpc_config.max_remotepc_unattend_minutes) && (no_sub_time >= 0))
                    return true;
                else
                    return false;

            } // IsOnduty()


            /**
             * 判断是否需要发送数据**/
            public bool IsValidPublish(ref AWSIOTCoreSDK.Adapter adapter)
            {
                // 当远程端在线时
                if (IsOnduty(ref adapter))
                {
                    int span_seconds = CalSpanSeconds(adapter.last_pub_time, DateTime.Now);
                    if ((span_seconds < adapter.hostpc_config.onduty_pub_span_seconds))
                        return false;
                    else
                        return true;
                }
                else
                {
                    // 远程设备不在线时
                    int span_minutes = CalSpanMinutes(adapter.last_pub_time, DateTime.Now);
                    if (span_minutes < adapter.hostpc_config.unonduty_pub_span_minutes)
                        return false;
                    else
                        return true;
                }

            } // IsvalidatePublish()
            

            /**
             * 计算两个时间之间的间隔秒数。**/
            static public int CalSpanSeconds(DateTime start_time, DateTime end_times)
            {
                TimeSpan span = new TimeSpan(end_times.Ticks - start_time.Ticks);
                return Convert.ToInt32(span.TotalSeconds);

            } // CalSpanSeconds()


            /**
             * 计算两个时间之间的间隔分钟数。**/
            static public int CalSpanMinutes(DateTime start_time, DateTime end_times)
            {
                TimeSpan span = new TimeSpan(end_times.Ticks - start_time.Ticks);
                return Convert.ToInt32(span.TotalMinutes);

            } // CalSpanMinutes()


            /**
             * 计算两个时间之间的间隔小时数。**/
            static public int CalSpanHours(DateTime start_time, DateTime end_times)
            {
                TimeSpan span = new TimeSpan(end_times.Ticks - start_time.Ticks);
                return Convert.ToInt32(span.TotalHours);

            } // CalSpanHours()


            /**
             * 计算两个时间之间的间隔天数。**/
            static public int CalSpanDays(DateTime start_time, DateTime end_times)
            {
                TimeSpan span = new TimeSpan(end_times.Ticks - start_time.Ticks);
                return Convert.ToInt32(span.TotalDays);

            } // CalSpanDays()


            /**
             * 创建客户端的唯一标识符**/
            static public string GetClientID()
            {
                string create_id = Guid.NewGuid().ToString();
                string client_id = "hostpc_" + create_id.Substring(0, 8);
                return client_id;

            } // GetClientID()


            /**
             * 保持当前控制台框一直打开。*/
            private static void KeepConsoleAppRunning(Action onShutdown)
            {
                manualResetEvent = new ManualResetEvent(false);
                Console.WriteLine("Press CTRL + C or CTRL + Break to exit...");
                Console.CancelKeyPress += (sender, e) =>
                {
                    onShutdown();
                    e.Cancel = true;
                    manualResetEvent.Set();
                };
                manualResetEvent.WaitOne();

            } // KeepConsoleAppRunning()

        } // class MqttContactAwsIOT_Application


    } // namespace AWSIOTCoreSDK

} // namespace HostPc
